<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Request;

class OrderProduct extends Backend
{
    public $navTitle = 'order';

    public function __construct()
    {
        parent::__construct();

        $this->OrderModel = model('Order.Order');
        $this->OrderProModel = model('Order.OrderProduct');
    }

    public function index()
    {
        $orderProductList = $this->OrderProModel->with(['user', 'product', 'order'])->paginate(10);

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'orderProductList' => $orderProductList
        ]);
    }

    // TODO 回收站
    public function appman()
    {
        $orderProList = $this->OrderProModel->onlyTrashed()->with('order')->paginate(10);

        if ($this->request->isAjax()) {
            $formData = $this->request->param();

            $action = $this->request->param('action');

            if ($action == 'return') {
                $OrderProId = $this->request->param('OrderProId', '');

                $OrderProduct = $this->OrderProModel->onlyTrashed()->find($OrderProId);

                if (!$OrderProduct) $this->error('订单商品不存在');

                $OrderProduct->restore() === FALSE ? $this->error('订单商品还原失败') : $this->success('订单商品还原成功');
            }

            if ($action == 'returns') {
                // TODO 转存idArr
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                $result = $this->OrderProModel->onlyTrashed()->whereIn('id', $idArr)->update(['deletetime' => null]);

                $result === FALSE ? $this->error('订单商品还原失败,' . $this->OrderProModel->getError()) : $this->success('订单商品还原成功');
            }

            // ? 还差删除子模型Order的数据
            if ($action == 'del') {
                $OrderProId = $this->request->param('OrderProId', '');

                $OrderProduct = $this->OrderProModel->onlyTrashed()->find($OrderProId);

                if (!$OrderProduct) $this->error('订单商品不存在');

                $result = $this->OrderProModel->destroy($OrderProId, true);

                $result === FALSE ? $this->error('订单商品删除失败,' . $this->OrderProModel->getError()) : $this->success('订单商品删除成功');
            }

            // ? 还差删除子模型Order的数据
            if ($action == 'dels') {
                // TODO 接收所有数据
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                $result = $this->OrderProModel->destroy($idArr, true);

                $result === FALSE ? $this->error('订单商品删除失败' . $this->OrderProModel->getError()) : $this->success('订单商品删除成功');
            }
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'orderProList' => $orderProList
        ]);
    }

    public function search()
    {
        $search_field = $this->request->param('search_field');

        $keyword = $this->request->param('keyword');

        $where = [];

        if ($search_field == 'nickname') $where['user.nickname'] = ['like', "%{$keyword}%"];
        if ($search_field == 'code') $where['order.code'] = $keyword;
        if ($search_field == 'shopname') $where['product.name'] = ['like', "%{$keyword}%"];

        $orderProductList = $this->OrderProModel->with(['user', 'product', 'order'])->where($where)->paginate(10);

        return $this->fetch('index', [
            'navTitle' => $this->navTitle,
            'orderProductList' => $orderProductList
        ]);
    }

    public function read($id)
    {
        //
    }

    public function edit($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        //
    }

    // TODO 软删除
    public function delete()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '');

            if ($action == 'del') {
                $OrderProId = $this->request->param('OrderProId', '');

                $OrderProuct = $this->OrderProModel->find($OrderProId);

                if (!$OrderProuct) $this->error('订单商品不存在');

                $result = $this->OrderProModel->destroy($OrderProId);

                $result === FALSE ? $this->error('订单商品移入回收站失败') : $this->success('订单商品移入回收站成功', url('admin/order_product/index'));
            }

            if ($action == 'dels') {
                // TODO 接收所有数据
                $formData = $this->request->param();
                // TODO 转存idArr
                $idArr = $formData['idArr'];

                $result = $this->OrderProModel->destroy($idArr);

                $result === FALSE ? $this->error('订单商品移入回收站失败') : $this->success('订单商品移入回收站成功');
            }
        }
    }
}
